﻿using System;
using System.Windows;
using Client.Commands;
using Client.Processes;
using Client.ViewModels;
using Client.Views;
using NLog;

namespace Client
{
    /// <summary>
    /// Interaktionslogik für "App.xaml"
    /// </summary>
    public partial class App : Application
    {
        protected Logger logger = LogManager.GetCurrentClassLogger(); 

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            try
            {
                //View
                var view = new MainView();
                view.DataContext = MainViewModel.Default;

                //Processes
                var import = new ImportMovies();
                var loadMovie = new LoadMovie();
                var loadEditMovie = new LoadMovie();
                var saveMovie = new SaveMovie();

                //Bind
                import.OnCompleted += m => MainViewModel.Default.ImportCompleted(m);              
                loadMovie.OnCompleted += m =>  MainViewModel.Default.AddMovieDetailsWorkspace(m, false);
                loadEditMovie.OnCompleted += m => MainViewModel.Default.AddMovieDetailsWorkspace(m, true);
                saveMovie.OnCompleted += m => MainViewModel.Default.AddMovieDetailsWorkspace(m, false);

                //Commands
                MainViewModel.Default.LoadMovieCommand = new LoadMovieCommand(loadMovie);
                MainViewModel.Default.EditMovieCommand = new EditMovieCommand(loadEditMovie);
                MainViewModel.Default.SaveMovieCommand = new SaveMovieCommand(saveMovie);

            
                MainViewModel.Default.ActiveWorkspace.IsBusy = true;

                import.Process();
                MainWindow.Show();
            }
            catch (Exception exc)
            {
                logger.Error(exc.Message);
                MessageBox.Show("Es ist ein Fehler aufgetreten. Nähere Informationen im Protokoll des Annwendungsverzeichnis. ", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                Shutdown();
            }
        }
    }
}
